﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.code.Actions
{
    class CustomTemplateAction : Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.T4Action
    {
        #region Input Properties
        // Place input properties here  
        #endregion



        /// <summary>  
        /// Generates a text file in the specified project.  
        /// </summary>  
        /// <param name="fileName">Name of the file to create.</param>  
        /// <param name="content">Content of the text file to be created.</param>  
        private void GenerateTextFile(string fileName, string content)
        {
            using (Microsoft.Practices.RecipeFramework.Library.Actions.AddItemFromStringAction myAction = new Microsoft.Practices.RecipeFramework.Library.Actions.AddItemFromStringAction())
            {
                base.Container.Add(myAction);

                // set parameters for action  
                myAction.Content = content;
                myAction.TargetFileName = fileName;                

                myAction.Execute();

            }
        }
        /// <summary>  
        /// Renders a templates and saves to a file.  
        /// </summary>  
        /// <param name="templateName">Name of text template file.</param>  
        /// <param name="fileOutput">Name of the output file to save the template to.</param>  
        private void GenerateTemplate(string templateName, string fileOutput)
        {

            // Get path to Template File  
            string newpath = System.IO.Path.Combine(base.GetTemplateBasePath(), templateName);

            // Read all text in Template File  
            String templateContent = System.IO.File.ReadAllText(newpath);

            // Render template  
            try
            {
                string ret = base.Render(templateContent, newpath);
                GenerateTextFile(fileOutput, ret);

            }

            catch (Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TemplateException te)
            {
                // Template error  
            }
            catch (Exception e)
            {
                // Other exception  
            }
        }

        public override void Execute()
        {

            // This arugment will be available to every text template  
           // base.additionalArguments.Add("Argument1", SomeArugment);


            //foreach (String c in ListOfClassesObject)
            //{
                base.additionalArguments.Add("MyClass", "");

                GenerateTemplate("Bind.cs.t4", "" + ".txt");

                // Remove MyClass so next iteartion of the loop gets   
                // a new MyClass parameter  
                base.additionalArguments.Remove("MyClass");
            //}

        }

        public override void Undo()
        {
        }
    }
}
